package ToYesterday.Day16_searchF;

import org.junit.Test;

import java.util.Arrays;

public class SearchF {
    @Test
    public void test(){
        int [] ints = {3,4,3,2,1};
        System.out.println(findPeakElement(ints));
    }
    public int findPeakElement(int[] nums) {
        int [] numsd =Arrays.copyOf(nums,nums.length);
//        for (int i = 0;i<nums.length;i++)numsd[i]=nums[i];
        sort(nums,0,nums.length-1);
        int i = search(numsd,nums[nums.length-1]);
        return i;
    }
    public int search(int [] nums,int i ){
        for (int j=0;j<nums.length;j++){
            if (nums[j]==i)return j;
        }
        return 0;

    }

    public void sort(int []nums,int start, int stop){
        if (start==stop)return;
        int middle = start+(stop-start)/2;
        sort(nums,start,middle);
        sort(nums,middle+1,stop);
        merge(nums,start,middle,stop);
    }
    public void merge(int [] nums,int start,int middle,int stop){
        int startd = start;
        int stopd = stop;
        int [] numtemp = new int[stop+1];
        int k=start;
        int nl = middle+1;
        while (start<=middle&&nl<=stop){
            if (nums[start]<nums[nl]){
                numtemp[k]=nums[start];
                k++;
                start++;
            }else {
                numtemp[k]=nums[nl];
                k++;
                nl++;
            }
        }
        while (start<=middle){
            numtemp[k]=nums[start];
            k++;
            start++;
        }
        while (nl<=stop){
            numtemp[k]=nums[nl];
            k++;
            nl++;
        }
        for (int i = startd;i<=stopd;i++){
            nums[i]=numtemp[i];
        }

    }
}
